package com.auto.qinglong.ui.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavHostController
import com.auto.base.net.ResultWrapper
import com.auto.base.ui.HandleUiEvent
import com.auto.base.util.ToastUtils
import com.auto.qinglong.R
import com.auto.qinglong.ui.viewmodel.LoginViewModel
import com.auto.qinglong.ui.viewmodel.QlViewModel

/**
 * Created by YuanWei1 on 2025/6/12.
 *
 * Description:
 */
@Composable
fun LoginScreen(navHostController: NavHostController, loginSuccess: () -> Unit = {}) {
    val viewModel: LoginViewModel = hiltViewModel()
    val appViewModel: QlViewModel = hiltViewModel()

    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    val uiViewModeState by viewModel.viewModelState.collectAsStateWithLifecycle()
    HandleUiEvent(viewModel, navHostController)
    val context = LocalContext.current
    LaunchedEffect(uiState) {
        when (uiState.callback) {
            is ResultWrapper.Error -> {
                ToastUtils.showNetError(context, uiState.callback as ResultWrapper.Error)
            }

            is ResultWrapper.Success<*> -> loginSuccess()
            null -> {

            }
        }
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(start = 30.dp, end = 30.dp, top = 100.dp, bottom = 60.dp)
    ) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // Logo Image
            Image(
                painter = painterResource(id = R.mipmap.ic_launcher_foreground),
                contentDescription = "App Logo",
                modifier = Modifier
                    .width(100.dp)
                    .height(100.dp)
                    .padding(bottom = 20.dp),
                contentScale = ContentScale.Crop
            )

            // Input Container
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(color = Color.White, shape = RoundedCornerShape(8.dp))
                    .padding(horizontal = 15.dp, vertical = 20.dp)
            ) {

                OutlinedTextField(
                    value = uiViewModeState.name,
                    onValueChange = { viewModel.updateUserName(it) },
                    label = { Text("用户名") },
                    modifier = Modifier.fillMaxWidth()
                )
                Spacer(modifier = Modifier.height(16.dp))
                OutlinedTextField(
                    value = uiViewModeState.pwd,
                    onValueChange = viewModel::updatePwd,
                    label = { Text("密码") },
                    visualTransformation = PasswordVisualTransformation(),
                    modifier = Modifier.fillMaxWidth()
                )
                OutlinedTextField(
                    value = uiViewModeState.ip,
                    onValueChange = viewModel::updateIp,
                    label = { Text("Ip") },
                    modifier = Modifier.fillMaxWidth()
                )
                // Buttons Row
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Button(
                        onClick = {
                            viewModel.saveIp()
                            appViewModel.login()
                            viewModel.login {
                                loginSuccess()
                                viewModel.saveNamePwd()
                            }
                        },
                        modifier = Modifier
                            .weight(1f)
                            .padding(end = 10.dp),
                    ) {
                        Text(text = stringResource(R.string.action_login), fontSize = 18.sp)
                    }
                }
            }
        }
    }
}




